#!/usr/bin/python
#coding=utf-8

from web.session import Session
def import_cls(fullname):
    parts = fullname.split('.')
    pkg, classname = '.'.join(parts[:-1]), parts[-1]
    obj = __import__(pkg, None, None, classname, 0)
    return getattr(obj, classname)

def md5(passwd, salt='netease'):
    import hashlib
    return hashlib.md5(passwd.lower()+salt).hexdigest()   

def auth(func):
    def dec(self, req, resp):
        sid = req.cookie().get('s_id')
        session = Session(req)
        if not sid:
            return resp.redirect(req, '/demo/login')
        if not session.get('login'):
            session.invalidate()
            return resp.redirect(req, '/demo/login')
        return func(self, req, resp)
    return dec
        
def check_token(func):
    def dec(self, req, resp):
        token = req.args.get('token')
        session = Session(req)
        if not token or token != session['token']: 
            return resp.redirect(req, '/demo/login')
        return func(self, req, resp)
    return dec


        



